package com.nexgo.payment.action.ecloglist;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.nexgo.oaf.apiv3.emv.EmvCardLogEntity;
import com.nexgo.payment.R;
import com.nexgo.payment.action.Action;
import com.nexgo.payment.action.ActionResult;
import com.nexgo.payment.action.ActionStep;
import com.nexgo.payment.action.OnActionListener;
import com.nexgo.payment.adapter.EcLogListAdapter;
import com.nexgo.payment.constrant.SysConstant;
import com.nexgo.payment.databases.Repository;
import com.nexgo.payment.ui.BaseFragment;
import com.nexgo.payment.util.ToastUtils;
import com.nexgo.payment.widget.RecyclerDivider;
import com.nexgo.payment.widget.TitleBuilder;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 电子现金明细查询界面
 *
 * @author 谭忠扬-YuriTam
 * @time 2017年9月4日
 */
public class EcLogListFragment extends BaseFragment implements Action<ActionStep>, EcLogListContract.View, BaseFragment.DownTimerListener {

    @BindView(R.id.rv_ec_log_list)
    RecyclerView rvEcLogList;
    @BindView(R.id.title_tv_right)
    TextView tvTitleRight;
    Unbinder unbinder;

    private OnActionListener mListener;
    private EcLogListContract.Presenter mPresenter;
    private EcLogListAdapter mListAdapter;
    private List<EmvCardLogEntity> mLogEntityList;

    public EcLogListFragment() {
        // Required empty public constructor
    }

    public static EcLogListFragment newInstance() {
        EcLogListFragment fragment = new EcLogListFragment();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        new EcLogListPresenter(this, Repository.getInstance());
    }

    @Override
    protected View initView(LayoutInflater inflater, ViewGroup container) {
        View view = inflater.inflate(R.layout.fragment_ec_log_list, container, false);
        unbinder = ButterKnife.bind(this, view);

        initTitle(view);

        return view;
    }

    //初始化标题栏
    private void initTitle(View view) {
        new TitleBuilder(view)
                .setExternalTitleBgRes(R.drawable.bg_blue_icon)
                .setLeftImage(R.drawable.arrow_icon)
                .setTitleText(getString(R.string.ec_list_title))
                .build();
    }

    @Override
    protected void initEvent() {}

    @Override
    protected void initData() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        rvEcLogList.setLayoutManager(layoutManager);
        rvEcLogList.addItemDecoration(new RecyclerDivider(getContext(),15,RecyclerDivider.HORIZONTAL));

        mPresenter.addEcLogList(mLogEntityList);
        //计时开始
        setDownTimer(SysConstant.TIME_OUT_60, this);
    }

    @OnClick({R.id.title_iv_left})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.title_iv_left:
                if (mListener != null) {
                    mListener.setActionResult(new ActionResult(this, ActionResult.ERR_CANCEL, null));
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnActionListener) {
            mListener = (OnActionListener) context;
        }
    }

    @Override
    public ActionStep getActionStep() {
        return ActionStep.EC_LOG_LIST;
    }

    @Override
    public void setPresenter(EcLogListContract.Presenter presenter) {
        mPresenter = presenter;
    }

    @Override
    public void addEcLogList(List<EmvCardLogEntity> logEntityList) {
        this.mLogEntityList = logEntityList;
    }

    @Override
    public void showEcLogList(List<EmvCardLogEntity> logEntityList) {
        if (mListAdapter == null){
            mListAdapter = new EcLogListAdapter(getContext());
            rvEcLogList.setAdapter(mListAdapter);
        }
        mListAdapter.addLogList(logEntityList);
    }

    @Override
    public void showEmpty() {
        ToastUtils.showToast(getString(R.string.has_no_trans_record));
    }

    @Override
    public boolean isActive() {
        return isAdded();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        cancelDownTimer();
        unbinder.unbind();
    }

    @Override
    public void onTick(long secondsUntilFinished) {
        if (!isActive()) {
            return;
        }
        tvTitleRight.setVisibility(View.VISIBLE);
        tvTitleRight.setText(String.valueOf(secondsUntilFinished));
    }

    @Override
    public void onFinish() {
        if (isActive()) {
            tvTitleRight.setText("0");
        }
        if (mListener != null) {
            mListener.setActionResult(new ActionResult(this, ActionResult.ERR_CANCEL, null));
        }
    }
}
